#include "shell_input.h"
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

int shell_input(const int argc, const char* argv[], const char* tpl, ...)
{
    if (argc != (strlen(tpl) + 1))
    {
        return 0;
    }

    va_list in;
    va_start(in, tpl);
#ifdef _MSC_VER
    struct SafeStringInput* s_holder;
#endif // _MSC_VER

    for (int i = 1; i < argc; i++)
    {
        switch (tpl[i - 1])
        {
        case 'd':
            *(va_arg(in, int*)) = atoi(argv[i]);
            break;
        case 'e':
            *(va_arg(in, double*)) = atof(argv[i]);
            break;
        case 'c':
            *(va_arg(in, char*)) = argv[i][0];
            break;
        case 's':
        #ifdef _MSC_VER
            s_holder = va_arg(in, struct SafeStringInput*);
            strcpy_s(s_holder->s, s_holder->space_size, argv[i]);
        #else
            strcpy(va_arg(in, char*), argv[i]);
        #endif // _MSC_VER
            break;
        default:
            va_end(in);
            return -1;  //  unkown input type
        }
    }

    va_end(in);
    return argc - 1;
}
